; Copyright 2013 padnest@gmail.com

; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
;   http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.

sr_loadPwmRegisters				; SUB ROUTINE: load rnd values in all PWM registers
	call	sr_nextRnd
	movwf	PWM_REG + 0
	call	sr_nextRnd
	movwf	PWM_REG + 1
	call	sr_nextRnd
	movwf	PWM_REG + 2
	call	sr_nextRnd
	movwf	PWM_REG + 3
	call	sr_nextRnd
	movwf	PWM_REG + 4
	call	sr_nextRnd
	movwf	PWM_REG + 5
	call	sr_nextRnd
	movwf	PWM_REG + 6
	call	sr_nextRnd
	movwf	PWM_REG + 7
	return


sr_nextRnd					; SUB ROUTINE: loadn in W register a rnd value from 0x20 to 0xFF
	rrf		RNDH			; Galois LFSR (linear feedback shift register) method
	rrf		RNDL
	bnc		sr_nextRnd_skip
	movlw	0xB4
	xorwf	RNDH
sr_nextRnd_skip
	movf	RNDL, W

sr_nextRnd_check1
	movwf	XREG
	sublw	48					; if w >= 48
	btfsc	STATUS, C
	goto	sr_nextRnd_check2
	movlw	0x80				; max value
	return

sr_nextRnd_check2
	movwf	XREG
	sublw	16					; if w >= 16
	btfsc	STATUS, C
	goto	sr_nextRnd_check3
	movlw	0x60				; medium value
	return

sr_nextRnd_check3
	movwf	XREG
	sublw	4					; if w >= 4
	btfsc	STATUS, C
	goto	sr_nextRnd_end
	movlw	0x40				; low value
	return

sr_nextRnd_end
	movlw	0x20				; very low value
	return


sr_testPwmRegisters				; SUB ROUTINE: load in all PWM registers some fixed values only for test
	movlf	0x10, PWM_REG + 0
	movlf	0x20, PWM_REG + 1
	movlf	0x30, PWM_REG + 2
	movlf	0x40, PWM_REG + 3
	movlf	0x50, PWM_REG + 4
	movlf	0x60, PWM_REG + 5
	movlf	0x70, PWM_REG + 6
	movlf	0x80, PWM_REG + 7
	return


